/*
 * @Description: 表数据查询
 * @Author: wangqi
 * @Date: 2024-07-25 21:59:20
 * @LastEditTime: 2024-07-26 18:25:01
 * @LastEditAuthor: wangqi
 */

const { db, info } = require("../config/index");
const logger = require("../_unit/logs");

/* 插入数据 */
exports.updateAccount = function (param) {
  return new Promise((resolve, reject) => {
    try {
      let { id, title, time, type, account, remarks } = param;
      let sql = `insert into record (id, title,time,type,account,remarks) values('${id}', '${title}', '${time}', '${type}', '${account}', '${remarks}')`;

      db.query(sql, (err, result) => {
        if (err) {
          reject(info.error("新增失败!"));
        } else {
          resolve(info.sucess({ msg: "ok" }, "新增成功!"));
        }
      });
    } catch {
      reject(info.error("新增异常!"));
    }
  });
};

/* 查找数据 */
exports.findAccount = function (param) {
  return new Promise((resolve, reject) => {
    try {
      let sql = `select * from record`;
      db.query(sql, (err, data) => {
        if (err) {
          reject(info.error("查询用户信息失败"));
        } else {
          resolve(info.sucess(data, "成功"));
        }
      });
    } catch {
      reject(info.error("查询用户信息异常"));
    }
  });
};

/* 删除数据 */
exports.delAccount = function (id) {
  return new Promise((resolve, reject) => {
    try {
      let sql = `delete from record WHERE id = '${id}'`;
      db.query(sql, (err, data) => {
        if (err) {
          reject(info.error("删除失败!"));
        } else {
          resolve(info.sucess(null, "删除成功！"));
        }
      });
    } catch {
      reject(info.error("删除异常！"));
    }
  });
};

exports.findList = function () {
  return new Promise((resolve, reject) => {
    try {
      let sql = `select * from record`;
      db.query(sql, (err, data) => {
        if (err) {
          reject(info.error("查询用户信息失败"));
        } else {
          resolve(info.sucess(data, "成功"));
        }
      });
    } catch {
      reject(info.error("查询用户信息异常"));
    }
  });
};
